import timer
import network
import pickle
import time

class CHandleCommand(object):
    def __init__(self):
        self.m_NextSecond = int(time.time()*100)
        self.m_FrameCommandList = []
        timer.Call_Out_Per_Second(self.SendMsg, 1)

    def OnHandle(self, lstPackage):
        for iFileNo, packageInfo in lstPackage:
            self.OnCommand(iFileNo, packageInfo)

    def OnCommand(self, iFileNo, PackageInfo):
        print("OnCommand", iFileNo, PackageInfo)
        sMsg = PackageInfo.get("msg", "")
        sPackage = pickle.dumps({"msg":sMsg, "no":iFileNo})
        self.m_FrameCommandList.append(sPackage)

    def SendMsg(self):
        self.m_NextSecond = int(time.time()*100)
        sAllMsg = pickle.dumps(self.m_FrameCommandList)
        if self.m_FrameCommandList:
            print("SendMsg")
            network.SendAll(sAllMsg)
        self.m_FrameCommandList = []
        timer.Call_Out_Per_Second(self.SendMsg, 1)

def Init():
    global g_HandleCommand
    g_HandleCommand = CHandleCommand()

def GetHandleCommand():
    global g_HandleCommand
    return g_HandleCommand